//
//  HSYBarButton.m
//  HSYCustomNavigationController
//
//  Created by key on 2017/2/9.
//  Copyright © 2017年 Scho.CustomNavigationController.Demo. All rights reserved.
//

#import "HSYBarButton.h"
#import "HSYNavigationItem.h"
#import <ReactiveCocoa.h>

@interface HSYBarButton ()

@end

@implementation HSYBarButton

#pragma mark - Update ButtonImageView && ButtonTitle Frame

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if (self.imageRect.size.width == 0) {
        return [super imageRectForContentRect:contentRect];
    }
    return self.imageRect;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    if (self.titleRect.size.width == 0) {
        return [super titleRectForContentRect:contentRect];
    }
    return self.titleRect;
}

@end

@implementation HSYBarButton (Show)

+ (HSYBarButton *)showBarButtonByPropertyParams:(NSDictionary <NSNumber *, id>*)params navigationItem:(HSYNavigationItem *)item imageRect:(CGRect)imageRect titleRect:(CGRect)titleRect didClickCompleted:(void(^)(HSYBarButton *button))completed
{
    HSYBarButton *button = [HSYBarButton buttonWithType:UIButtonTypeCustom];
    button.titleRect = titleRect;
    button.imageRect = imageRect;
    button.barItem = item;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIImage *image = params[@(kHSYCustomBarButtonPropertyTypeImage)];
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
    }
    
    UIImage *hImage = params[@(kHSYCustomBarButtonPropertyTypeHightImage)];
    if (hImage) {
        [button setImage:hImage forState:UIControlStateHighlighted];
    }
    
    NSString *title = params[@(kHSYCustomBarButtonPropertyTypeTitle)];
    if (title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateHighlighted];
    }
    
    UIFont *font = params[@(kHSYCustomBarButtonPropertyTypeFont)];
    if (font) {
        button.titleLabel.font = font;
    }
    
    UIColor *color = params[@(kHSYCustomBarButtonPropertyTypeTextColor)];
    if (color) {
        [button setTitleColor:color forState:UIControlStateNormal];
    }
    
    UIColor *hColor = params[@(kHSYCustomBarButtonPropertyTypeHightTextColor)];
    if (hColor) {
        [button setTitleColor:hColor forState:UIControlStateHighlighted];
    }
    
    [[[button rac_signalForControlEvents:UIControlEventTouchUpInside] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(HSYBarButton *button) {
        if (completed) {
            completed(button);
        }
    }];
    
    return button;
}

@end
